package com.shaozi.nc.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.shaozi.nc.execpool.MakeBriefReportReadMQRunnable;
import com.shaozi.nc.execpool.MakeOperationReadMQRunnable;
import com.shaozi.nc.execpool.MakeSecretaryReadMQRunnable;
import com.shaozi.nc.manage.secretary.BriefReportManage;
import com.shaozi.nc.manage.secretary.OperationManage;
import com.shaozi.nc.manage.secretary.SecretaryManage;

/**
 * 通用mq manage aop入口 
 * BriefReportManage 
 * SecretaryManage
 * @author lyx
 */
@Component
@Aspect
public class MQManageAop extends BaseManageAop {

	private final Logger logger = Logger.getLogger(MQManageAop.class);

	public MQManageAop() {

	}

	@Pointcut(value = "execution(public boolean com.shaozi.nc.manage.secretary..*.makeSecretaryRead(..))")
	public void cutExecute() {
		// logger.debug("point cut");
	}

	@AfterReturning(pointcut = "cutExecute()", returning = "retVal")
	public void afterExecute(JoinPoint jp, boolean retVal) {
		commonAfterExecute(true, jp, retVal);
	}

	@Pointcut(value = "execution(public boolean com.shaozi.nc.manage.secretary..*.makeSecretaryReadByObserver(..))")
	public void cutExecuteObserver() {
		// logger.debug("point cut");
	}

	@AfterReturning(pointcut = "cutExecuteObserver()", returning = "retVal")
	public void afterExecuteObserver(JoinPoint jp, boolean retVal) {
		commonAfterExecute(false, jp, retVal);
	}

	private void commonAfterExecute(boolean afterExecute, JoinPoint jp, boolean retVal) {
		Class targetClass = jp.getTarget().getClass();
		if (targetClass.equals(SecretaryManage.class)) {
			if (afterExecute) {
				baseAfterExecute(jp, retVal, MakeSecretaryReadMQRunnable.class);
			} else {
				baseAfterExecuteObserver(jp, retVal, MakeSecretaryReadMQRunnable.class);
			}
		} else if (targetClass.equals(BriefReportManage.class)) {
			if (afterExecute) {
				baseAfterExecute(jp, retVal, MakeBriefReportReadMQRunnable.class);
			} else {
				baseAfterExecuteObserver(jp, retVal, MakeBriefReportReadMQRunnable.class);
			}
		} else if (targetClass.equals(OperationManage.class)) {
			if (afterExecute) {
				baseAfterExecute(jp, retVal, MakeOperationReadMQRunnable.class);
			} else {
				baseAfterExecuteObserver(jp, retVal, MakeOperationReadMQRunnable.class);
			}
		} else {
			logger.warn("请instance 注册aop 记录发送三端");
		}
	}

}
